1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20  import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
21  import static com.google.common.collect.testing.features.CollectionFeature.RESTRICTS_ELEMENTS;
22  import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_ADD;
23  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
24  import static java.util.Collections.singletonList;
25  
26  import com.google.common.annotations.GwtCompatible;
27  import com.google.common.collect.testing.AbstractCollectionTester;
28  import com.google.common.collect.testing.MinimalCollection;
29  import com.google.common.collect.testing.features.CollectionFeature;
30  import com.google.common.collect.testing.features.CollectionSize;
31  
32  import java.util.ConcurrentModificationException;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * A generic JUnit test which tests addAll operations on a collection. Can't be
38   * invoked directly; please see
39   * {@link com.google.common.collect.testing.CollectionTestSuiteBuilder}.
40   *
41   * @author Chris Povirk
42   * @author Kevin Bourrillion
43   */
44  @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
45  @GwtCompatible(emulated = true)
46  public class CollectionAddAllTester<E> extends AbstractCollectionTester<E> {
47    @CollectionFeature.Require(SUPPORTS_ADD)
48    public void testAddAll_supportedNothing() {
49      assertFalse("addAll(nothing) should return false",
50          collection.addAll(emptyCollection()));
51      expectUnchanged();
52    }
53  
54    @CollectionFeature.Require(absent = SUPPORTS_ADD)
55    public void testAddAll_unsupportedNothing() {
56      try {
57        assertFalse("addAll(nothing) should return false or throw",
58            collection.addAll(emptyCollection()));
59      } catch (UnsupportedOperationException tolerated) {
60      }
61      expectUnchanged();
62    }
63  
64    @CollectionFeature.Require(SUPPORTS_ADD)
65    public void testAddAll_supportedNonePresent() {
66      assertTrue("addAll(nonePresent) should return true",
67          collection.addAll(createDisjointCollection()));
68      expectAdded(samples.e3, samples.e4);
69    }
70  
71    @CollectionFeature.Require(absent = SUPPORTS_ADD)
72    public void testAddAll_unsupportedNonePresent() {
73      try {
74        collection.addAll(createDisjointCollection());
75        fail("addAll(nonePresent) should throw");
76      } catch (UnsupportedOperationException expected) {
77      }
78      expectUnchanged();
79      expectMissing(samples.e3, samples.e4);
80    }
81  
82    @CollectionFeature.Require(SUPPORTS_ADD)
83    @CollectionSize.Require(absent = ZERO)
84    public void testAddAll_supportedSomePresent() {
85      assertTrue("addAll(somePresent) should return true",
86          collection.addAll(MinimalCollection.of(samples.e3, samples.e0)));
87      assertTrue("should contain " + samples.e3, collection.contains(samples.e3));
88      assertTrue("should contain " + samples.e0, collection.contains(samples.e0));
89    }
90  
91    @CollectionFeature.Require(absent = SUPPORTS_ADD)
92    @CollectionSize.Require(absent = ZERO)
93    public void testAddAll_unsupportedSomePresent() {
94      try {
95        collection.addAll(MinimalCollection.of(samples.e3, samples.e0));
96        fail("addAll(somePresent) should throw");
97      } catch (UnsupportedOperationException expected) {
98      }
99      expectUnchanged();
100   }
101 
102   @CollectionFeature.Require({SUPPORTS_ADD,
103       FAILS_FAST_ON_CONCURRENT_MODIFICATION})
104   @CollectionSize.Require(absent = ZERO)
105   public void testAddAllConcurrentWithIteration() {
106     try {
107       Iterator<E> iterator = collection.iterator();
108       assertTrue(collection.addAll(MinimalCollection.of(samples.e3, samples.e0)));
109       iterator.next();
110       fail("Expected ConcurrentModificationException");
111     } catch (ConcurrentModificationException expected) {
112       // success
113     }
114   }
115 
116   @CollectionFeature.Require(absent = SUPPORTS_ADD)
117   @CollectionSize.Require(absent = ZERO)
118   public void testAddAll_unsupportedAllPresent() {
119     try {
120       assertFalse("addAll(allPresent) should return false or throw",
121           collection.addAll(MinimalCollection.of(samples.e0)));
122     } catch (UnsupportedOperationException tolerated) {
123     }
124     expectUnchanged();
125   }
126 
127   @CollectionFeature.Require(value = {SUPPORTS_ADD,
128       ALLOWS_NULL_VALUES}, absent = RESTRICTS_ELEMENTS)
129   public void testAddAll_nullSupported() {
130     List<E> containsNull = singletonList(null);
131     assertTrue("addAll(containsNull) should return true", collection
132         .addAll(containsNull));
133     /*
134      * We need (E) to force interpretation of null as the single element of a
135      * varargs array, not the array itself
136      */
137     expectAdded((E) null);
138   }
139 
140   @CollectionFeature.Require(value = SUPPORTS_ADD,
141       absent = ALLOWS_NULL_VALUES)
142   public void testAddAll_nullUnsupported() {
143     List<E> containsNull = singletonList(null);
144     try {
145       collection.addAll(containsNull);
146       fail("addAll(containsNull) should throw");
147     } catch (NullPointerException expected) {
148     }
149     expectUnchanged();
150     expectNullMissingWhenNullUnsupported(
151         "Should not contain null after unsupported addAll(containsNull)");
152   }
153 
154   @CollectionFeature.Require(SUPPORTS_ADD)
155   public void testAddAll_nullCollectionReference() {
156     try {
157       collection.addAll(null);
158       fail("addAll(null) should throw NullPointerException");
159     } catch (NullPointerException expected) {
160     }
161   }
162 }
163